package com.example.springmvc.demo;

import com.example.springmvc.User;
import com.example.springmvc.user.Person;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 666
 * Date: 2023-07-30
 * Time: 22:27
 */
@Controller
//标记类为控制器，处理用户请求
@ResponseBody
//将方法返回的对象直接作为响应体返回给客户端
@RequestMapping("/user")
public class UserDemo {
    @RequestMapping("/zy")
    public String zy(){
        return "hello world";
    }
    @RequestMapping(value = "/hi")
public  Object getHi(@RequestParam(value = "n",defaultValue = "zcx") String name){
        System.out.println(name+"你好");
    return name+"你好！";
}
    @GetMapping("/example/{id}")
    public String handleGetRequest(@PathVariable("id") Long id) {
        // 处理逻辑，id 是从 URL 路径中获取的参数值
        System.out.println("id="+id);
        return "viewName";
    }
    @RequestMapping("file")
    public String FileUpload(String name,@RequestPart("file") MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
      // ⽂件保存地址
        String filePath = ClassUtils.getDefaultClassLoader().getResource("stat ic").getPath() +
                "/" + UUID.randomUUID() + fileName;
      // 保存⽂件
        file.transferTo(new File(filePath));
        return "上传成功！"+filePath;
    }
    @RestController
    public class UserController {

        @PostMapping("/createUser")
        public String createUser(@RequestBody User user) {
            // 处理逻辑，user对象将会从请求体中获取JSON数据
            return "User created successfully!";
        }
    }

    @RequestMapping("js")
public Object getJson( Person person){
    System.out.println("Person：" + person);
    return "redirect:/index.html";
}
@RequestMapping("/per")
 public  Object getHi( Person person){
    System.out.println("对象中的 name："+person.getName());
    System.out.println("对象中的 password："+person.getPassword());
    return "/index.html";
 }
    @RequestMapping("/m3")
    public Object method_3(String name, @RequestParam("password") String pwd) {
        System.out.println("name 参数：" + name);
        System.out.println("pwd 参数：" + pwd);
        return "/index.html";
    }
}
